package hongcaosp.app.android.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blackchopper.loginshare.impl.LoginShare;

import hongcaosp.app.android.MainActivity;
import hongcaosp.app.android.R;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.utils.TaskPool;

/**
 * 找回密码设置密码
 */
public class PasswordResetActivity extends BaseActivity implements ICodeView,ILoginView {
    private EditText passwordET;
    private EditText codeET;
    private TextView codeBTN,textCenter;

    private String phone;

    private LoginPresenter loginPresenter;

    @Override
    protected void initData(Intent intent) {
        phone = intent.getStringExtra("mobile");

        textCenter.setText("已向手机+86" + phone + "发送验证码");
        codeBTN.setEnabled(false);
        codeCount(60);
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);

        findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViewById(R.id.sure_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureAction();
            }
        });

        passwordET = findViewById(R.id.password_et);
        codeET = findViewById(R.id.code_et);
        codeBTN = findViewById(R.id.code_btn);

        textCenter = findViewById(R.id.text_center);

        codeBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codeAction();
            }
        });

        loginPresenter = new LoginPresenter(this,this);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_password_reset;
    }

    private void sureAction() {
        String password = passwordET.getText().toString();
        String code = codeET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().showToast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastManager.getInstance().showToast("请输入密码");
            return;
        }
        if (password.length() < 8) {
            ToastManager.getInstance().showToast("密码长度在8～20位噢");
            return;
        }
        loginPresenter.updatePassword(phone,code,password);
    }

    private void codeAction() {
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        codeBTN.setEnabled(false);
        loginPresenter.sendPasswordCode(phone);
    }

    private void codeCount(int count) {
        final int n = count - 1;
        if (n >= 0) {
            codeBTN.setText(n + "S" );
            TaskPool.myPool().postTaskOnMainDelay(new Runnable() {
                @Override
                public void run() {
                    codeCount(n);
                }
            },1000);
        } else {
            codeBTN.setEnabled(true);
            codeBTN.setText("获取验证码");
        }
    }

    @Override
    public void onCodeSend(boolean success) {
        if (success) {
            codeCount(60);
        } else {
            codeBTN.setEnabled(true);
        }
    }

    @Override
    public void onLogged(boolean isOk, String message) {
        if (isOk) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else {
            ToastManager.getInstance().showToast(message);
        }
    }

    public static void toActivity(Context context, String phone) {
        if (context != null) {
            Intent intent = new Intent(context,PasswordResetActivity.class);
            intent.putExtra("mobile",phone);
            context.startActivity(intent);
        }
    }
}
